import data
import re
import matplotlib.pyplot as plt


def plotassub(x, y, titleandlabel, subnum):
    plt.setp(axs[subnum].get_xticklabels(), fontsize=16, family='Times New Roman')
    plt.setp(axs[subnum].get_yticklabels(), fontsize=16, family='Times New Roman')
    axs[subnum].plot(x, y, 'ro', markersize=15, alpha=0.6)
    axs[subnum].set_title(titleandlabel[0], fontsize=16, family='Times New Roman', rotation=45)
    axs[subnum].set_xlabel(titleandlabel[1], fontsize=16, family='Times New Roman')
    axs[subnum].set_ylabel(titleandlabel[2], fontsize=16, family='Times New Roman')


SamplingF = 2*10**9  # sampling frequency
Signal = []  # list to save Class_signal
IndependantV = []  # independent variable. !diff in diff test!
DependatV = {}  # dependent variables. calculated
# select file and read csv
S, Pathname = data.readcsv()
del S
for i in Pathname:
    Sdata, P = data.readcsv(i)
    Signal.append(data.signal(Sdata, SamplingF))
    del P, Sdata
    # regular expression to extract needed num in path
    numlist = re.findall("-?\d+\d*", i)  # include the negative number point canot be added
    # print(numlist)
    # test 1 corresponding to energy
    # !!! need modification for different test !!!
    IndependantV.append(int(numlist[1]))
    S_RMS, S_var, S_skew, S_kurtosis = Signal[-1].parameters_Nth(2)
    DependatV.setdefault('Kurtosis', []).append(S_kurtosis)   # index here means the ith signal in csv
    DependatV.setdefault('RMS', []).append(S_RMS)
    DependatV.setdefault('Skew', []).append(S_skew)
    del S_kurtosis, S_RMS, S_skew, S_var

del SamplingF
Signal[2].mapsignal()

# print(DependatV)
#
# print(Pathname)
# print(IndependantV)
# print(DependatV)

fig, axs = plt.subplots(3, 1)
fig.tight_layout()  # adjust the blank
plt.subplots_adjust(wspace=0, hspace=0.1)  # adjust the space of subfigs
title = [
    ['Kurtosis', ' ', 'Vpp/RMS'],
    ['RMS', ' ', 'RMS'],
    ['Skew', 'Incident angle(degree)', '$k_3//sigma^3$']]   # Consistent with the previous dictionary definition

for i in range(len(title)):
    print(i)
    plotassub(IndependantV, DependatV[title[i][0]], title[i], i)

fig.suptitle('Signal characteristics corresponding to the laser incident angle', fontsize=16, family='Times New Roman')

plt.show()
